package org.elanfox.intellicommunity.biz.service.core.statistics.impl;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.biz.entity.statistics.AlarmStatisticsPO;
import org.elanfox.intellicommunity.biz.repository.statistics.AlarmStatisticsRepository;
import org.elanfox.intellicommunity.biz.service.core.statistics.AlarmStatisticsService;
import org.elanfox.intellicommunity.common.utils.Jsr310Utils;
import org.elanfox.intellicommunity.data.base.base.AbstractSearchableCrudService;
import org.elanfox.intellicommunity.data.base.base.SpecificationAndSort;
import org.elanfox.intellicommunity.data.base.base.SqlDataReader;
import org.elanfox.intellicommunity.data.base.dto.BaseSaveDTO;
import org.elanfox.intellicommunity.data.base.dto.BaseSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/10/13
 */
@Service
@Slf4j
public class AlarmStatisticsServiceImpl extends AbstractSearchableCrudService<AlarmStatisticsPO, Long> implements AlarmStatisticsService {
    private AlarmStatisticsRepository alarmStatisticsRepository;

    @Autowired
    protected AlarmStatisticsServiceImpl(AlarmStatisticsRepository repository) {
        super(repository);
        this.alarmStatisticsRepository = repository;
    }

    @Override
    public void statistics(LocalDate date) {
        if (alarmStatisticsRepository.existsByDate(date)) {
            log.info("今日警报数量已统计，日期：" + date);
            return;
        }


        save(statisticsPO(date));
    }

    private AlarmStatisticsPO statisticsPO(LocalDate date) {
        AlarmStatisticsPO po = new AlarmStatisticsPO();
        po.setDate(date);

        LocalDateTime start = date.atStartOfDay();
        LocalDateTime end = Jsr310Utils.Date.endOfDay(date);

        String sqlWhere = " and create_time >= '" + Jsr310Utils.DateTime.asString(start) + "' and create_time <='" + Jsr310Utils.DateTime.asString(end) + "'";

        //总数量
        String sql = "select ifnull(count(1),0) as num from ic_alarm_msg_group where 1=1 " + sqlWhere;
        SqlDataReader reader = executeReader(sql);
        int count = reader.getInteger("num");
        po.setTotalCount(count);
        //已处理
        sql = "select ifnull(count(1),0) as num from ic_alarm_msg_group " +
                "where alarm_status=2||alarm_status=3 " + sqlWhere;
        reader = executeReader(sql);
        count = reader.getInteger("num");
        po.setTotalHandled(count);
        //警报解除
        sql = "select ifnull(count(1),0) as num from ic_alarm_msg_group " +
                "where alarm_status=4 " + sqlWhere;
        reader = executeReader(sql);
        count = reader.getInteger("num");
        po.setIgnoreCount(count);

        sql = "select ifnull(count(1),0) as num from ic_alarm_msg_group where alarm_type=3" + sqlWhere;
        reader = executeReader(sql);
        count = reader.getInteger("num");
        po.setSpecialCount(count);

        return po;
    }

    @Override
    public List<AlarmStatisticsPO> sevenDays() {
        LocalDate end = LocalDate.now().minusDays(1);
        LocalDate begin = end.minusDays(6);

        List<AlarmStatisticsPO> list = alarmStatisticsRepository.findByDateIsBetween(begin, end);
        //然后今天的实时统计
        AlarmStatisticsPO po = statisticsPO(LocalDate.now());
        list.add(po);

        return list;
    }

    @Override
    protected SpecificationAndSort<AlarmStatisticsPO> specificationAndSort(BaseSearchDTO searchDTO) {
        return null;
    }

    @Override
    public AlarmStatisticsPO save(BaseSaveDTO saveDTO) {
        return null;
    }
}
